﻿@{
    ViewBag.Title = "Chat";
}

<h4>Chat</h4>

    <div class="chatwindow"style="height:120px;width:1000px;border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;">
        <ul id="discussion">
        </ul>
    </div>
    <div class="sendbox" width:220px;padding:10px;border:5px;margin:0px;position:absolute;bottom:0;>
            <input type="text" id="message" width:800px/>
            <input type="button" id="sendmessage" value="Send" />
            <input type="hidden" id="displayname" />
    </div>

@section scripts {
    <script src="~/Scripts/jquery.signalR-1.0.1.js"></script>
    <script src="~/signalr/hubs"></script>
    <!-- to update page og send messange -->
    <script>
        $(function () {
            var chat = $.connection.chatHub;
            chat.client.addNewMessageToPage = function (name, message) {
                //bæta skilabodum
                $('#discussion').append('<li><strong>' + htmlEncode(name) + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            //skifa nafn
            $('#displayname').val(prompt('Enter your name:', ''));
            //UserId.IsAnonymous == false;
            
            //foucus skilabod
            $('#message').focus();

            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    //kallar á send
                    chat.server.send($('#displayname').val(), $('#message').val());
                   //clear text box and foucus next comment
                    $('#message').val('').focus();
                    });
               });
        });
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }

    </script>


}